package com.netcracker.ui.command;

import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.AuthorService;
import com.netcracker.entity.Author;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class ReadAuthorCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadAuthorCommand.class);
    private final ServiceFacade serviceFacade;

        public ReadAuthorCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        AuthorService authorService = serviceFacade.getAuthorService();
        Integer authorID = Integer.parseInt(request.getParameter("authorID"));
        Author author = null;
        try {
            author = authorService.findAuthorById(authorID);
        } catch (DataTransmittingException ex) {
            request.setAttribute("error", "Ошибка обращения к БД.");
            LOGGER.error("DB error: ", ex);
        } catch (NullDataException ex) {
            request.setAttribute("error", "Автор не найден.");
        }
        request.setAttribute("author", author);
        return "author.jsp";
    }
    
}